home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / stdg44.exe / lha / PROGRESS.C < prev    next >
C/C++ Source or Header  |  1994-01-10  |  2KB  |  86 lines

  1. /*
  2.  *  Draw a progress bar when the user presses a mouse button.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "stdg.h"
  7.  
  8. /* Number of steps to perform in progress bar drawing */
  9. #define NSTEPS 23
  10.  
  11. /* Total time in seconds to complete progress bar */
  12. #define TOTALTIME 10
  13.  
  14. /* Time in millisecs for each step */
  15. #define STEPTIME (TOTALTIME*1000/NSTEPS)
  16.  
  17. rectangle rcenter(rectangle r1, rectangle r2) /* center r1 on r2 */
  18. {
  19.     long w1, h1, w2, h2;
  20.  
  21.     w1 = dx(r1);
  22.     h1 = dy(r1);
  23.     w2 = dx(r2);
  24.     h2 = dy(r2);
  25.  
  26.     return rdiag(r2.min.x + (w2-w1)/2, r2.min.y + (h2-h1)/2, w1, h1);
  27. }
  28.  
  29. void draw_progress(bitmap *b)
  30. {
  31.     int i;
  32.     point p;
  33.     char buf[100];
  34.     rectangle r, r2;
  35.  
  36.     r = rect(0,0,200,sys_font->height+6);
  37.     r = rcenter(r, b->r);
  38.  
  39.     start_timer(STEPTIME);
  40.  
  41.     for (i=0; i<=NSTEPS; i++)
  42.     {
  43.         /* clear and draw the rectangle */
  44.         draw_rect(b, r, -1, BLACK);
  45.         fill_rect(b, r, WHITE);
  46.  
  47.         /* print a completion percentage into a string */
  48.         sprintf(buf, "%d%% Complete", i*100/NSTEPS);
  49.  
  50.         /* find the string size */
  51.         p = strsize(sys_font, buf);
  52.  
  53.         /* center the string */
  54.         p = addp(divp(subp(subp(r.max, r.min), p), 2), r.min);
  55.  
  56.         /* draw it at the calculated point */
  57.         draw_string(b, p, sys_font, buf, BLACK);
  58.  
  59.         /* draw the progress bar */
  60.         r2 = r;
  61.         r2.max.x = r.min.x + (dx(r)*i/NSTEPS);
  62.         fill_rect(b, r2, BLUE | notDorS);
  63.  
  64.         /* wait for next timer event */
  65.         get_timer();
  66.     }
  67.  
  68.     /* stop the timer */
  69.     start_timer(0);
  70. }
  71.  
  72. int main(int argc, char **argv)
  73. {
  74.     window *w;
  75.     mouse m;
  76.  
  77.     ginit("Progress Bar Demo", NULL, NULL);
  78.     w = new_window("Progress!", rdiag(50,50,250,60), Titlebar | Maximize);
  79.     show_window(w);
  80.     while (1) {
  81.         m = get_mouse(w);
  82.         if (m.kind & MouseUp)
  83.             draw_progress(w->b);
  84.     }
  85.     return 0;
  86. }